﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class FinishMasterData : BaseFunction
    {


        public FinishMasterData()
            : base()
        { 

       }
        public FinishMasterData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet GetFinishMaster(DataSet dsTarget, DataRow dwWAFARBEN)
        {
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText ="SELECT WAART " +
                ",WAVAR"+
                ",WAAENB" +
                ",WAPROGRP" +
                ",WAARTGRP" +
                ",WARSTAKZ" +
                ",WAVWART" +
                ",WAVWVAR" +
                ",WAFFDDIK" +
                ",WAFFDDIS" +
                ",WAFDDIM" +
                ",WARFDDIK" +
                ",WARFDDIS" +
                ",WAFBRTL" +
                ",WARWBRT" +
                ",WAFGRQM" +
                ",WARGRQM" +
                ",WAFGRLFD" +
                ",WARGRLFD" +
                ",WABIND" +
                ",WAEINZNR" +
                ",WARSTDLG" +
                ",WARSTDGW" +
                ",WARFDDIS" +
                ",WALAEAEND as FEnlongation" +                  
                " FROM INTEX.WASTAMM WHERE WAART =:ArticleNum AND WAVAR=:ArticleVar";
                 orlReadCommand.Parameters.Add("ArticleNum", OracleType.NVarChar).Value =dwWAFARBEN["WAFART"].ToString();
               orlReadCommand.Parameters.Add("ArticleVar", OracleType.NVarChar).Value =dwWAFARBEN["WAFVAR"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "WASTAMM";
                    Queue Q10=new Queue();
                    Q10.Enqueue("WAFBRTL");
                    Q10.Enqueue("WARWBRT");
                    Queue Q100=new Queue();
                    Q100.Enqueue("WAFFDDIK");
                    Q100.Enqueue("WAFFDDIS");
                    Q100.Enqueue("WARFDDIK");
                    Q100.Enqueue("WARFDDIS");
                    Q100.Enqueue("FEnlongation");
                    Queue Q1000=new Queue();
                    Q1000.Enqueue("WARSTDLG");
                    Q1000.Enqueue("WARSTDGW");
                    
                    DivideBy10(dsResult.Tables[0], Q10);
                    DivideBy100(dsResult.Tables[0], Q100);
                    DivideBy1000(dsResult.Tables[0], Q1000);
                    
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }

            }
            catch (Exception ex)
            {
                 Tracer.Error(ex.StackTrace, ex); 
            }
            return dsTarget;
        }
        public DataSet GetFinishColorWay(DataSet dsTarget, DataRow dwWAFARBEN)
        {
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT WAFART, WAFVAR,WAFZUART,WAFZUSATZ,WAFBEZ" +
                    " FROM INTEX.WAFARBEN WHERE WAFART = :ArticleNum AND WAFVAR=:ArticleVar ";
                //AND WAFZUSATZ=:ArticleColor";
                orlReadCommand.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwWAFARBEN["WAFART"].ToString();
                orlReadCommand.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwWAFARBEN["WAFVAR"].ToString();
                //orlReadCommand.Parameters.Add("ArticleColor", OracleType.NVarChar).Value = dwWAFARBEN["WAFZUSATZ"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "WAFINISH";
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains("WAFINISH"))
                    {
                        dsTarget.Tables["WAFINISH"].Clear();
                        dsTarget.AcceptChanges();
                    }
               }
                dsTarget = ImportDataTable(dsTarget
                , FilterByColor(
                dsResult.Tables["WAFINISH"],
                "WAFZUSATZ",
                dwWAFARBEN["WAFZUSATZ"].ToString())
                ); 
              
            }
            catch (Exception ex)
            {
                 Tracer.Error(ex.StackTrace, ex); 
            }
            return dsTarget;
        }
        public DataSet LoadWAASTAMM(string UsingID)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                UsingID = UsingID.Trim();
                UsingID = UsingID.Replace("\t", "");
                UsingID =  Left(UsingID + "        ", 8);
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct WAART as  ArtNum, WAVAR as ArtVar,WABEZ as ArtDes"
               + " FROM INTEX.WASTAMM WHERE  WAKUNDNR='00000000' AND WAART like '%%' "
                + " AND (WANEUB = '" + UsingID + "' OR '" + UsingID + "' IN ('FPDE    ','XHJUN   ')) order by 1 desc";

                setResult = Connect2Proxy(orlReadCommand);

                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "ARTICLE";

                    byte[] b;
                    string value;
                    foreach (DataRow dw in setResult.Tables[0].Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["ArtDes"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["ArtDes"] = value;
                    } 
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet BuildFinishMasterData(DataSet dsTarget, DataRow dwWAFARBEN)
        {
            try
            {
                dsTarget = GetFinishColorWay(dsTarget, dwWAFARBEN);  
            }
            catch (Exception ex)
            {
                 Tracer.Error(ex.StackTrace, ex); 
            }
            return dsTarget;
        }
    }
}